package com.zheng.wechat.mp.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.wechat.mp.dao.model.WechatMpGzuser;
import com.zheng.wechat.mp.dao.model.WechatMpGzuserExample;
import com.zheng.wechat.mp.dao.model.WechatMpSystemAccount;
import com.zheng.wechat.mp.dao.model.WechatMpSystemAccountExample;
import com.zheng.wechat.mp.rpc.api.WechatMpGzuserService;
import com.zheng.wechat.mp.rpc.api.WechatMpSystemAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/8.
 */
@Controller
@RequestMapping("/manage/gzaccount")
@Api(value = "用户账号控制器", description = "用户账号控制器")
public class GzAccountController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GzAccountController.class);

    @Autowired
    private WechatMpSystemAccountService wechatMpSystemAccountService;
    @Autowired
    private WechatMpGzuserService wechatMpGzuserService;
    @Autowired
    private UpmsApiService upmsApiService;
    /**
     * 文章首页
     * @return
     */
    @ApiOperation(value = "微信用户账号首页")
    @RequiresPermissions("wechatmp:gzaccount:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/gzaccount/index.jsp";
    }


    @ApiOperation(value = "微信用户账号列表")
    @RequiresPermissions("wechatmp:gzaccount:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, defaultValue = "0", value = "userId") int userId) {
        WechatMpSystemAccountExample wechatMpSystemAccountExample = new WechatMpSystemAccountExample();
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            wechatMpSystemAccountExample.setOrderByClause(sort + " " + order);
        }
        WechatMpSystemAccountExample.Criteria criteria = wechatMpSystemAccountExample.createCriteria();
        if (0 < userId) {
            criteria.andUserIdEqualTo(userId);
        }
        //限制代理
        String loginUserName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(loginUserName);
        if(agentId!=null && agentId!= Constant.CONST_SUPER_AGENTID){
            WechatMpGzuserExample wechatMpGzuserExample = new WechatMpGzuserExample();
            wechatMpGzuserExample.createCriteria().andUserAgentIdEqualTo(agentId);
            List<WechatMpGzuser> userList = wechatMpGzuserService.selectByExample(wechatMpGzuserExample);
            List<Integer> userIdList = new ArrayList<Integer>();
            for(int i=0;i<userList.size();i++ ){
                userIdList.add(userList.get(i).getId());
            }
            criteria.andUserIdIn(userIdList);
        }
        List<WechatMpSystemAccount> rows = wechatMpSystemAccountService.selectByExampleForOffsetPage(wechatMpSystemAccountExample, offset, limit);
        long total = wechatMpSystemAccountService.countByExample(wechatMpSystemAccountExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }
}

